package com.ychug.gateway.cms;

import java.util.List;

import com.ychug.framework.service.SystemSerialNumber;
import com.ychug.service.cms.entity.CmsNewsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ychug.service.cms.service.ICmsNewsService;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.common.base.ResultAjaxHash;

/**
 * 内容 信息操作处理
 * 
 * @author mcpcms
 * @date 2019-04-30
 */
@Controller
@RequestMapping("/cms/cmsNews")
public class CmsNewsController extends BaseController
{
    private String prefix = "modules/cms/news";
	
	@Autowired
	private ICmsNewsService cmsNewsService;

	@Autowired
	private SystemSerialNumber systemSerialNumber;

	@GetMapping()
	public String cmsNews()
	{
	    return prefix + "/viewNews";
	}
	
	/**
	 * 查询内容列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CmsNewsEntity cmsNews)
	{
		startPage();
        List<CmsNewsEntity> list = cmsNewsService.selectCmsNewsList(cmsNews);
		return getDataTable(list);
	}
	
	/**
	 * 新增内容
	 */
	@GetMapping("/add")
	public String add()
	{
		String no = systemSerialNumber.createSerialNumber("ContentNo","News");
		return prefix + "/addNews";
	}
	
	/**
	 * 新增保存内容
	 */
	@PostMapping("/add")
	@ResponseBody
	public ResultAjaxHash addSave(CmsNewsEntity cmsNews)
	{		

		return toAjax(cmsNewsService.insertCmsNews(cmsNews));
	}

	/**
	 * 修改内容
	 */
	@GetMapping("/edit/{contentId}")
	public String edit(@PathVariable("contentId") Integer contentId, ModelMap mmap)
	{
		CmsNewsEntity cmsNews = cmsNewsService.selectCmsNewsById(contentId);
		mmap.put("cmsNews", cmsNews);
	    return prefix + "/editNews";
	}
	
	/**
	 * 修改保存内容
	 */
	@PostMapping("/edit")
	@ResponseBody
	public ResultAjaxHash editSave(CmsNewsEntity cmsNews)
	{		
		return toAjax(cmsNewsService.updateCmsNews(cmsNews));
	}
	
	/**
	 * 删除内容
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public ResultAjaxHash remove(String ids)
	{		
		return toAjax(cmsNewsService.deleteCmsNewsByIds(ids));
	}
	
}
